<template>
   <a-spin :spinning="confirmLoading">
     <j-form-container :disabled="formDisabled">
       <!-- 主表单区域 -->
       <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
         <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="供应商编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入供应商编码" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="供应商全称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入供应商全称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="供应商简称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shortName">
              <a-input v-model="model.shortName" placeholder="请输入供应商简称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="供应商类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="providerType">
              <j-dict-select-tag type="list" v-model="model.providerType"  dictCode="purchase_provider_type" placeholder="请选择供应商类型" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="经营范围" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="businessScope">
              <j-multi-select-tag type="checkbox" v-model="model.businessScope"  dictCode="purchase_goods_type" placeholder="请选择经营范围" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="供应商状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="providerStatus">
              <j-dict-select-tag type="list" v-model="model.providerStatus"  dictCode="purchase_provider_status" placeholder="请选择供应商状态" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="公司邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="companyMail">
              <a-input v-model="model.companyMail" placeholder="请输入公司邮箱" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="省市区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="province">
		      <j-area-linkage type="cascader" v-model="model.province" placeholder="请输入省市区" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="详细地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactSite">
              <a-textarea v-model="model.contactSite" v-decorator="['contactSite']" rows="4" placeholder="请输入详细地址" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="开户行" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="openingBank">
              <a-input v-model="model.openingBank" placeholder="请输入开户行" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="收款账户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentAccount">
              <a-input v-model="model.paymentAccount" placeholder="请输入收款账户" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="发票类型 " :labelCol="labelCol" :wrapperCol="wrapperCol" prop="invoiceType">
              <j-dict-select-tag type="list" v-model="model.invoiceType"  dictCode="purchase_invoice_type" placeholder="请选择发票类型 " />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="发票抬头" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="invoiceTitle">
              <a-input v-model="model.invoiceTitle" placeholder="请输入发票抬头" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="税率  " :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taxRate">
              <j-dict-select-tag type="list" v-model="model.taxRate"  dictCode="purchase_tax_rate" placeholder="请选择税率  " />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="结算方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="clearingForm">
              <a-input v-model="model.clearingForm" placeholder="请输入结算方式" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="纳税人识别号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taxpayerIdentificationNumber">
              <a-input v-model="model.taxpayerIdentificationNumber" placeholder="请输入纳税人识别号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="法定代表人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="legalRepresentative">
              <a-input v-model="model.legalRepresentative" placeholder="请输入法定代表人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="注册资本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="registeredCapital">
              <a-input v-model="model.registeredCapital" placeholder="请输入注册资本" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="实缴资本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contributedCapital">
              <a-input v-model="model.contributedCapital" placeholder="请输入实缴资本" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="企业性质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enterpriseNature">
              <a-input v-model="model.enterpriseNature" placeholder="请输入企业性质" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="注册地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="registeredAddress">
              <a-textarea v-model="model.registeredAddress" v-decorator="['registeredAddress']" rows="4" placeholder="请输入注册地址" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="成立时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="establishedTime">
              <j-date placeholder="请选择成立时间" v-model="model.establishedTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" v-decorator="['remark']" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="审核状态 " :labelCol="labelCol" :wrapperCol="wrapperCol" prop="processStatus">
              <j-dict-select-tag type="radio" v-model="model.processStatus"  dictCode="purchase_process_status" placeholder="请选择审核状态 " />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
     </j-form-container>
      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="相关资质" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="purchaseProviderCertificationTable.loading"
            :columns="purchaseProviderCertificationTable.columns"
            :dataSource="purchaseProviderCertificationTable.dataSource"
            :maxHeight="300"
            :disabled="formDisabled"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
        <a-tab-pane tab="联系人" :key="refKeys[1]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[1]"
            :loading="purchaseProviderContactsTable.loading"
            :columns="purchaseProviderContactsTable.columns"
            :dataSource="purchaseProviderContactsTable.dataSource"
            :maxHeight="300"
            :disabled="formDisabled"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
        <a-tab-pane tab="提供的物品" :key="refKeys[2]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[2]"
            :loading="purchaseProviderGoodsTable.loading"
            :columns="purchaseProviderGoodsTable.columns"
            :dataSource="purchaseProviderGoodsTable.dataSource"
            :maxHeight="300"
            :disabled="formDisabled"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
      </a-tabs>
    </a-spin>
</template>

<script>

  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import {providerCodeRule} from '@/api/api'

  export default {
    name: 'PurchaseProviderForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
          code:''
        },
        validatorRules: {
          name: [
            { required: true, message: '请输入供应商全称'},
            { min: 5, max: 50, message: '长度在5到50个字符', tigger: 'blur'}
          ],
          providerType: [
            { required: true, message: '请选择供应商类型'},
          ],
          businessScope: [
            { required: true, message: '请至少选择一个经营范围'},
          ],
          providerStatus: [
            { required: true, message: '请选择供应商状态'},
          ],
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        refKeys: ['purchaseProviderCertification', 'purchaseProviderContacts', 'purchaseProviderGoods', ],
        tableKeys:['purchaseProviderCertification', 'purchaseProviderContacts', 'purchaseProviderGoods', ],
        activeKey: 'purchaseProviderCertification',
        // 相关资质
        purchaseProviderCertificationTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '企业资质证书名称',
              key: 'qualificationName',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '企业资质证书',
              key: 'qualification',
              type: FormTypes.file,
              token:true,
              responseName:"message",
              number: 10,
              width:"200px",
              placeholder: '请选择文件',
              defaultValue:'',
            },
            {
              title: '企业资质证书过期时间',
              key: 'qualificationExpirationTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '附件',
              key: 'attachment',
              type: FormTypes.file,
              token:true,
              responseName:"message",
              number: 10,
              width:"200px",
              placeholder: '请选择文件',
              defaultValue:'',
            },
          ]
        },
        // 联系人
        purchaseProviderContactsTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '联系人姓名',
              key: 'contactsName',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '请输入${title}'},],
            },
            {
              title: '联系人电话',
              key: 'contactsPhone',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '请输入${title}'},],
            },
            {
              title: '联系人职位',
              key: 'contactsPosition',
              type: FormTypes.select,
              dictCode:"purchase_contacts_position",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '联系人邮箱',
              key: 'contactsMail',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // 提供的物品
        purchaseProviderGoodsTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '物品编码',
              key: 'goodsCode',
              type: FormTypes.popup,
              popupCode:"purchase_goods",
              destFields:"goodsCode,goodsName,goodsType,unit,unitPrice",
              orgFields:"code,goods_name,goods_type_code,unit,unit_price",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '物品分类',
              key: 'goodsType',
              type: FormTypes.select,
              dictCode:"purchase_goods_type",
              disabled:true,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '物品名称',
              key: 'goodsName',
              type: FormTypes.input,
              disabled:true,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '单位',
              key: 'unit',
              type: FormTypes.select,
              dictCode:"purchase_unit",
              disabled:true,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '单价',
              key: 'unitPrice',
              type: FormTypes.input,
              disabled:true,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },

            {
              title: 'goods_code',
              key: 'goods_code',
              type:"hidden"
            },

            {
              title: 'goods_name',
              key: 'goods_name',
              type:"hidden"
            },

            {
              title: 'goods_type',
              key: 'goods_type',
              type:"hidden"
            },


            {
              title: 'unit_price',
              key: 'unit_price',
              type:"hidden"
            },
          ]
        },
        url: {
          add: "/purchase/purchaseProvider/add",
          edit: "/purchase/purchaseProvider/edit",
          purchaseProviderCertification: {
            list: '/purchase/purchaseProvider/queryPurchaseProviderCertificationByMainId'
          },
          purchaseProviderContacts: {
            list: '/purchase/purchaseProvider/queryPurchaseProviderContactsByMainId'
          },
          purchaseProviderGoods: {
            list: '/purchase/purchaseProvider/queryPurchaseProviderGoodsByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      //初始化供应商编码
      init(){
        providerCodeRule({}).then((res)=>{
          if(res.success){
            this.model.code = res.result;
          }else{
            this.$message.warning(res.message);
          }
        });
      },
     addBefore(){
            this.purchaseProviderCertificationTable.dataSource=[]
            this.purchaseProviderContactsTable.dataSource=[]
            this.purchaseProviderGoodsTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.purchaseProviderCertification.list, params, this.purchaseProviderCertificationTable)
          this.requestSubTableData(this.url.purchaseProviderContacts.list, params, this.purchaseProviderContactsTable)
          this.requestSubTableData(this.url.purchaseProviderGoods.list, params, this.purchaseProviderGoodsTable)
        }
        if(!this.model.id){
          this.init();
        }
      },
      //校验所有一对一子表表单
    validateSubForm(allValues){
        return new Promise((resolve,reject)=>{
          Promise.all([
          ]).then(() => {
            resolve(allValues)
          }).catch(e => {
            if (e.error === VALIDATE_NO_PASSED) {
              // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
              this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
            } else {
              console.error(e)
            }
          })
        })
    },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          purchaseProviderCertificationList: allValues.tablesValue[0].values,
          purchaseProviderContactsList: allValues.tablesValue[1].values,
          purchaseProviderGoodsList: allValues.tablesValue[2].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     close() {
        this.visible = false
        this.$emit('close')
        this.$refs.form.clearValidate();
      },

    }
  }
</script>

<style scoped>
</style>